package com.zhangtai.modules.service.impl.leave;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.LeaveInfoDao;
import com.zhangtai.modules.dto.LeaveInfoDto;
import com.zhangtai.modules.entity.leave.LeaveInfoEntity;
import com.zhangtai.modules.service.leave.LeaveInfoService;
import com.zhangtai.utils.Excel.EntityUtil;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@Slf4j
public class LeaveInfoServiceImpl extends ServiceImpl<LeaveInfoDao, LeaveInfoEntity> implements LeaveInfoService {

    @Autowired
    private LeaveInfoDao leaveInfoDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<LeaveInfoEntity> page = this.page(
                new Page<LeaveInfoEntity>(pageNum, pageSize),
                new QueryWrapper<LeaveInfoEntity>().like(!StringUtils.isEmpty(keyword), "name", keyword)
                        .eq("is_delete", 0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult getLeaveList(@RequestParam(value = "oid", required = false) Long oid,
                                   @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "endTime", required = false) String endTime,
                                   @RequestParam(value = "dealStatus", required = false) List<Long> dealStatus,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        IPage<LeaveInfoDto> page = new Page(pageNum, pageSize);
        List<LeaveInfoDto> list = leaveInfoDao.getLeaveList(oid, startTime, endTime, dealStatus, page);
        page.setRecords(list);
        return new PageResult<>(page);
    }

    @Override
    public List<LeaveInfoEntity> getSameTimeResult(@RequestParam(value = "startTime", required = false) String startTime,
                                                   @RequestParam(value = "endTime", required = false) String endTime,
                                                   @RequestParam(value = "oid", required = false) Long oid) {
        return this.list(new QueryWrapper<LeaveInfoEntity>().and(warp -> warp.and(war -> war.ge("leave_begin_time", startTime).le("leave_end_time", endTime)).
                or(war -> war.le("leave_begin_time", startTime).ge("leave_end_time", endTime)).
                or(war -> war.le("leave_begin_time", startTime).le("leave_end_time", endTime).ge("leave_end_time", startTime)).
                or(war -> war.ge("leave_begin_time", startTime).ge("leave_end_time", endTime).le("leave_begin_time", endTime))).
                eq("oid", oid).and(warp -> warp.eq("deal_status", 1).or().eq("deal_status", 2).eq("is_delete", 0).ne("type", 8)));
    }

    @Override
    public PageResult getLeaveListSystem(@RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                         @RequestParam(value = "unitIds", required = false) List<Long> unitIds,
                                         @RequestParam(value = "jobIds", required = false) List<Long> jobIds,
                                         @RequestParam(value = "orgId", required = false) List<Long> orgId,
                                         @RequestParam(value = "type", required = false) String type,
                                         @RequestParam(value = "name", required = false) String name,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<LeaveInfoDto> page = new Page(pageNum, pageSize);
        List<LeaveInfoDto> list = leaveInfoDao.getLeaveListSystem(orgIds, unitIds, jobIds, orgId, type, name, page);
        page.setRecords(list);
        return new PageResult<>(page);
    }

    @Override
    public Map<String, Object> getLeaveInfo(@RequestParam(value = "id", required = false) Long id) {
        return leaveInfoDao.getLeaveInfo(id);
    }

    @Override
    public LeaveInfoEntity getByIdE(Long id) {
        LeaveInfoEntity one = this.getOne(new QueryWrapper<LeaveInfoEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(LeaveInfoEntity entity) {
        boolean b = this.save(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(LeaveInfoEntity entity) {
        boolean b = this.updateById(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
        LeaveInfoEntity entity = new LeaveInfoEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()) {
                return r;
            }
        }
        return R.ok();
    }

}